package com.whoiszxl.query.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whoiszxl.core.base.BaseSupport;
import com.whoiszxl.core.entity.PageQuery;
import com.whoiszxl.core.entity.ResponseResult;
import com.whoiszxl.core.entity.response.PageResponse;
import com.whoiszxl.core.enums.StatusEnum;
import com.whoiszxl.core.utils.AssertUtils;
import com.whoiszxl.core.utils.AuthUtils;
import com.whoiszxl.core.utils.BeanUtil;
import com.whoiszxl.db.mapper.MemberInfoMapper;
import com.whoiszxl.db.mapper.MemberMapper;
import com.whoiszxl.db.model.MemberInfoPO;
import com.whoiszxl.db.model.MemberPO;
import com.whoiszxl.query.MemberQueryApplicationService;
import com.whoiszxl.query.model.qry.MemberQuery;
import com.whoiszxl.query.model.response.MemberDetailResponse;
import com.whoiszxl.query.model.response.MemberResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户查询应用服务接口实现
 *
 * @author whoiszxl
 * @date 2022/1/24
 */
@Service
@RequiredArgsConstructor
public class MemberQueryApplicationServiceImpl implements MemberQueryApplicationService {

    private final MemberMapper memberMapper;

    private final MemberInfoMapper memberInfoMapper;

    @Override
    public MemberResponse memberInfo() {
        Long memberId = AuthUtils.getMemberId();
        AssertUtils.isTrue(Objects.nonNull(memberId), "未登录");

        MemberPO memberPO = memberMapper.selectById(memberId);
        MemberInfoPO memberInfoPO = memberInfoMapper.selectOne(Wrappers.<MemberInfoPO>lambdaQuery().eq(MemberInfoPO::getMemberId, memberId));

        MemberResponse memberResponse = BeanUtil.copyProperties(memberPO, MemberResponse.class);
        BeanUtil.copyProperties(memberInfoPO, memberResponse);
        return memberResponse;
    }


    @Override
    public MemberDetailResponse memberDetailById(String memberId) {
        MemberPO memberPO = memberMapper.selectById(memberId);
        MemberInfoPO memberInfoPO = memberInfoMapper.selectOne(Wrappers.<MemberInfoPO>lambdaQuery().eq(MemberInfoPO::getMemberId, memberId));

        MemberDetailResponse memberDetailResponse = BeanUtil.copyProperties(memberPO, MemberDetailResponse.class);
        BeanUtil.copyProperties(memberInfoPO, memberDetailResponse);
        return memberDetailResponse;
    }

    @Override
    public PageResponse<MemberResponse> list(MemberQuery query) {
        QueryWrapper<MemberPO> queryWrapper = Wrappers.query();

        Class<?> queryClazz = query.getClass();
        List<Field> queryFieldList = BaseSupport.getClassField(queryClazz, new ArrayList<>());
        queryFieldList.forEach(field -> BaseSupport.buildQuery(query, field, queryWrapper));

        IPage<MemberPO> page = memberMapper.selectPage(query.toPage(), queryWrapper);
        PageResponse<MemberResponse> pageResponse = PageResponse.convert(page, MemberResponse.class);
        return pageResponse;
    }
}
